<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
	  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<meta charset="utf-8">
	<title>layuiAdmin 后台管理员</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
	<link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" media="all">
	<style>
		.layui-table-cell {
			height: auto;
			line-height: 30px;
		}
	</style>
</head>
<body>
<div class="layui-fluid">
	<div class="layui-card">
	<form action="" id="searchForm">
		<div class="layui-form layui-card-header layuiadmin-card-header-auto">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">类型</label>
					<div class="layui-input-block">
						<select name="type">
							<option value="">请选择</option>
							<option value="1">公告</option>
							<option value="2">关于</option>
						</select>
					</div>
				</div>

				<div class="layui-inline">
					<button type="button" class="layui-btn layui-btn-primary" lay-submit lay-filter="ZX_search_btn">
					<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
					</button>
				</div>

			</div>
		</div>
	</form>
		<div class="layui-card-body">
			<div style="padding-bottom: 10px;">
					<button class="layui-btn layui-btn-primary btnAdd">
						<i class="layui-icon layuiadmin-button-btn">&#xe654;</i>
					</button>
					<button class="layui-btn layui-btn-primary allDel">
						<i class="layui-icon layuiadmin-button-btn">&#xe640;</i>
					</button>
			</div>

			<table id="ZX_table_list" lay-filter="ZX_table_list"></table>
		</div>
	</div>
</div>

<script th:src="@{/layuiadmin/layui/layui.js}"></script>
<!-- 绑定按钮 -->
<script type="text/html" id="barZX">
	<a class="layui-btn layui-btn-primary" lay-event="edit">
		<i class="layui-icon layuiadmin-button-btn">&#xe642;</i>
	</a>
	<a class="layui-btn layui-btn-primary" lay-event="del">
		<i class="layui-icon layuiadmin-button-btn">&#xe640;</i>
	</a>
</script>

<script type="text/html" id="typeTemp">
	{{#  if(d.type=="1"){ }}
	公告
	{{#  } else if(d.type=="2"){ }}
	关于
	{{#  } }}

</script>

<script language="javascript">
layui.config({
	base: '../layuiadmin/'
}).extend({
	index: 'lib/index'
}).use(['index', 'table'], function(){
	var $ = layui.$,
	form = layui.form,
	table = layui.table;
	//初始化表格
	table.render({
		elem: '#ZX_table_list',
		height: 'full-180',
		url: '../blog/getNoticeList',
		page: true,
		cols: [
				[
				{checkbox: true, fixed:true},
				{field:'id', title:'ID', align:'center', width:80, sort:true},
				{field:'type', title:'类型',templet:'#typeTemp',width:80},
				{field:'content',title:'内容'},
				{title:'操作', align:'center', fixed:'right', width:220, toolbar:'#barZX'}
			]
		],done:function(res){
			tdTitle();
		}
	});

	function tdTitle(){
		$('th').each(function(index,element){
			$(element).attr('title',$(element).text());
		});
		$('td').each(function(index,element){
			$(element).attr('title',$(element).text());
		});
	};

	//搜索
	form.on('submit(ZX_search_btn)', function(data){
		var field = data.field;
		//执行重载
		table.reload('ZX_table_list', {
			page:1,//查询时 默认定位到第一页
			where: field
		});
		$('.layui-table-tips').hide();
	});

	//添加操作
	$(".btnAdd").click(function(){
		//弹出并修改
		layer.open({
			type: 2,
			title: '添加',
			content: '../blog/goNoticeAdd',
			area: ['90%', '90%'],
			btnAlign: 'c',
			btn: ['确定', '取消'],
			yes: function(index, layero){
				var iframeWindow = window['layui-layer-iframe'+ index],
				submitID = 'ZX_iframe_submit',
				submit = layero.find('iframe').contents().find('#'+ submitID);
				//监听提交
				iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
					console.log(data.field);
					$.ajax({
						type: "POST",
						url: '../blog/insertNotice',
						data: data.field,
						dataType: "json",
						success: function(data){
							if(data.code==0){
								layer.msg(data.msg);
								table.reload('ZX_table_list');
								layer.close(index);
							}else{
								layer.msg(data.msg);
							}
						}
					});
				});
				submit.trigger('click');
			}
		});
	});

	//批量删除
	$(".allDel").click(function(){
		var checkStatus = table.checkStatus('ZX_table_list'),
		checkData = checkStatus.data, //得到选中的数据
		ids = [];

		if(checkData.length===0){ return layer.msg('请选择要删除的数据');}

		for(var i in checkData){ ids.push(checkData[i]['id']); }

		layui.layer.confirm('您真的删除么', function(index){
			layui.layer.close(blogIndex);
			$.ajax({
				type: "GET",
				url: '../blog/deleteNoticeByIds?id='+ids.toString(),
				dataType: "json",
				success: function(data){
					layer.msg(data.msg);
					if(data.code==0){
						table.reload('ZX_table_list');
						layer.close(blogIndex);
					}
				}
			});
		});
	});

	//监听工具条
	table.on('tool(ZX_table_list)', function(obj){
		var data = obj.data,
		layEvent = obj.event;

		if(layEvent === 'del'){
			//删除操作
			layer.confirm('真的删除行么', function(index){
				$.ajax({
					type: "GET",
					url: '../blog/deleteNoticeByIds?id='+obj.data.id,
					dataType: "json",
					success: function(data){
						if(data.code==0){
							layer.msg(data.msg);
							table.reload('ZX_table_list');
							layer.close(blogIndex);
						}else{
							layer.msg(data.msg);
						}
					}
				});
			});
		}else if(layEvent === 'edit'){
			//弹出并修改
			layer.open({
				type: 2,
				title: '修改',
				content: '../blog/goNoticeEdit?id='+obj.data.id,
				area: ['90%', '90%'],
				btnAlign: 'c',
				btn: ['确定', '取消'],
				yes: function(index, layero){
					var iframeWindow = window['layui-layer-iframe'+ index],
					submitID = 'ZX_iframe_submit',
					submit = layero.find('iframe').contents().find('#'+ submitID);
					//监听提交
					iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
						console.log(data.field);
						$.ajax({
							type: "POST",
							url: '../blog/updateNoticeById',
							data: data.field,
							dataType: "json",
							success: function(data){
								if(data.code==0){
									layer.msg(data.msg);
									table.reload('ZX_table_list');
									layer.close(index);
								}else{
									layer.msg(data.msg);
								}
							}
						});
					});
					submit.trigger('click');
				}
			});
		}
	});
});
</script>
</body>
</html>

